module ApplicationHelper

  def format_datetime(datetime, style)
    datetime.strftime(style) if datetime
  end

  def header_for(action, flag="")

    html = ""
    if action == "home"
      html.concat(content_tag :li, content_tag(:a, t('home.home'), href: "/"), class: 'active')
    else
      html.concat(content_tag :li, content_tag(:a, t('home.home'), href: "/"))
    end

    if action == "posts"
      html.concat(content_tag :li, content_tag(:a, t('home.news'), href: "/posts"), class: 'active')
    else
      html.concat(content_tag :li, content_tag(:a, t('home.news'), href: "/posts"))
    end

    if action == "products"
      html.concat(content_tag :li, content_tag(:a, t('home.product'), href: '/products'), class: 'active')
    else
      html.concat(content_tag :li, content_tag(:a, t('home.product'), href: '/products'))
    end

    if action == "introduces"
      if flag == "service"
        html.concat(content_tag :li, content_tag(:a, t('home.service'), href: '/introduces/show/service'), class: 'active')
      else
        html.concat(content_tag :li, content_tag(:a, t('home.service'), href: '/introduces/show/service'))
      end

      if flag == "customer"
        html.concat(content_tag :li, content_tag(:a, t('home.customer'), href: '/introduces/show/customer'), class: 'active')
      else
        html.concat(content_tag :li, content_tag(:a, t('home.customer'), href: '/introduces/show/customer'))
      end

      if flag == "partner"
        html.concat(content_tag :li, content_tag(:a, t('home.partner'), href: '/introduces/show/partner'), class: 'active')
      else
        html.concat(content_tag :li, content_tag(:a, t('home.partner'), href: '/introduces/show/partner'))
      end

    else
      html.concat(content_tag :li, content_tag(:a, t('home.service'), href: '/introduces/show/service'))
      html.concat(content_tag :li, content_tag(:a, t('home.customer'), href: '/introduces/show/customer'))
      html.concat(content_tag :li, content_tag(:a, t('home.partner'), href: '/introduces/show/partner'))
    end

    if action == "about"
      html.concat(content_tag :li, content_tag(:a, t('home.about_us'), href: '/about/description'), class: 'active')
    else
      html.concat(content_tag :li, content_tag(:a, t('home.about_us'), href: '/about/description'))
    end

    raw html

  end

  def banner_for(banner)
    if !banner.blank?
      banner.img.url
    else
      image_url 'banner-news.png'
    end

  end

  def breadcrumb(controller,flag='')
    if controller == 'posts'
      title = t('home.news')
      second = title
      third = t('common.company_news')
    end

    if controller == 'products'
      title = t('home.product')
      second = title
      # third = t('common.product_descrip')
    end

    if controller == 'introduces'
      if flag == 'service'
        title = t('home.service')
        second = t('home.about_us')
        third = title
      end

      if flag == 'customer'
        title = t('common.projects')
        second = t('home.about_us')
        third = title
      end

      if flag == 'partner'
        title = t('home.partner')
        second = t('home.about_us')
        third = title
      end
    end

    if controller == 'about'
      if flag == 'description'
        title = t('common.company')
        second = t('home.about_us')
        third = t('common.company')
      end

      if flag == 'job'
        title = t('common.jobs')
        second = title
      end

      if flag == 'contact'
        title =  t('home.contact')
        second = t('home.about_us')
        third =  title
      end  
    end

    home = t('home.home')
    location = t('common.location')
    second_arrow = '&gt;' unless third.nil?

    first_html = <<EOF
<div class="breadcrumb">
  <h3>#{title}</h3>
  <span>
    <cite>#{location}：</cite>
    <ol>
      <li>#{home} </li>
      <li class="arrow">&gt;</li>
      <li>#{second} </li>
EOF

    if third.nil?
    second_html = <<EOF
    </ol>
  </span>
</div>
EOF

    else
      second_html = <<EOF
      <li class="arrow">#{second_arrow}</li>
      <li>#{third}</li>
    </ol>
  </span>
</div>
EOF
    end


    raw "#{first_html}#{second_html}"
  end

end
